package com.sloera.time;

import java.time.Instant;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.time.format.FormatStyle;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;
import java.util.TimeZone;

/**
 * 时间格式化
 * ┌─────────────┐
 * │LocalDateTime│────┐
 * └─────────────┘    │    ┌─────────────┐
 * ├───>│ZonedDateTime│
 * ┌─────────────┐    │    └─────────────┘
 * │   ZoneId    │────┘           ▲
 * └─────────────┘      ┌─────────┴─────────┐
 * │                   │
 * ▼                   ▼
 * ┌─────────────┐     ┌─────────────┐
 * │   Instant   │<───>│    long     │
 * └─────────────┘     └─────────────┘
 * <br/>
 * <p>新旧api同数据库时间映射</p>
 * 数据库	对应Java类（旧）	对应Java类（新）
 * DATETIME	java.util.Date	LocalDateTime
 * DATE	java.sql.Date	LocalDate
 * TIME	java.sql.Time	LocalTime
 * TIMESTAMP	java.sql.Timestamp	LocalDateTime
 *
 * @author liuwangyang
 * @version 1.0
 * @class com.sloera.time.Time
 * @date 2020-7-23
 */
public class Time {
    public static void main(String[] args) {
        //dateTimeFormatter();
        //instant();
        //oldConvertToNew();
        //newConvertToOld();
        long ts = 1574208900000L;
        System.out.println(timestampToString(ts, Locale.CHINA, "Asia/Shanghai"));
        System.out.println(timestampToString(ts, Locale.US, "America/New_York"));

    }

    /*
     * @Description 根据毫秒数计算时间
     * @param epochMilli: 毫秒数
     * @param lo: 格式化输出使用
     * @param zoneId: 转换时间使用
     * @Return String
     * @Author SloeraN
     * @Date 2020/7/23 21:27
     */
    private static String timestampToString(long epochMilli, Locale lo, String zoneId) {
        Instant instant = Instant.ofEpochMilli(epochMilli);
        DateTimeFormatter f = DateTimeFormatter.ofLocalizedDateTime(FormatStyle.MEDIUM, FormatStyle.SHORT);
        return f.withLocale(lo).format(ZonedDateTime.ofInstant(instant, ZoneId.of(zoneId)));

    }

    /**
     * 新api转旧api 通过 long
     */
    private static void newConvertToOld() {
        // ZonedDateTime -> long:
        ZonedDateTime zdt = ZonedDateTime.now();
        long ts = zdt.toEpochSecond() * 1000;
        // long -> Date:
        Date date = new Date(ts);
        // long -> Calendar:
        Calendar calendar = Calendar.getInstance();
        calendar.clear();
        calendar.setTimeZone(TimeZone.getTimeZone(zdt.getZone().getId()));
        calendar.setTimeInMillis(zdt.toEpochSecond() * 1000);
    }

    /**
     * 旧api转新api
     */
    private static void oldConvertToNew() {
        // Date -> Instant:
        Instant ins1 = new Date().toInstant();
        // Calendar -> Instant -> ZonedDateTime:
        Calendar calendar = Calendar.getInstance();
        Instant ins2 = Calendar.getInstance().toInstant();
        ZonedDateTime zdt = ins2.atZone(calendar.getTimeZone().toZoneId());
    }


    /**
     * 时间戳
     */
    private static void instant() {
        Instant now = Instant.now();
        // 秒
        System.out.println(now.getEpochSecond());
        System.out.println(String.valueOf(now.getEpochSecond()).length());
        //毫秒
        System.out.println(now.toEpochMilli());
        System.out.println(String.valueOf(now.toEpochMilli()).length());
        Instant ins = Instant.ofEpochSecond(1568568760);
        ZonedDateTime zdt = ins.atZone(ZoneId.systemDefault());
        System.out.println(zdt);
        System.out.println(ZoneId.getAvailableZoneIds());
    }

    /**
     * 时间格式化
     */
    private static void dateTimeFormatter() {
        ZonedDateTime zdt = ZonedDateTime.now();
        // ofPattern '' 中的内容可以原样打印
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm ZZZZ");
        System.out.println(formatter.format(zdt));
        DateTimeFormatter zhFormatter = DateTimeFormatter.ofPattern("yyyy MMM dd EE HH:mm", Locale.CHINA);
        System.out.println(zhFormatter.format(zdt));
        DateTimeFormatter usFormatter = DateTimeFormatter.ofPattern("E, MMMM/dd/yyyy HH:mm", Locale.US);
        System.out.println(usFormatter.format(zdt));
    }
}
